package Test;

import javax.ws.rs.core.MediaType;

import nl.tue.be.ds.duraark.bSDD.ClientUtilities;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;

public class TestSearchPerGuid {

	private String searchGuid = "2YUUCAWJqHu000025QrE$V";
	private final String searchByGuidUrl = "http://bsddpilot.catenda.no/api/4.0/IfdConcept/";
	
	
	public JsonNode searchPerGuid() throws Exception{
		String res = null;
		JsonNode jsonConcepts;
		Client client = ClientUtilities.getClient();

		WebResource searchCall = client.resource(searchByGuidUrl + searchGuid);
		
		res = getSearch(searchCall);
	
		if (res != null) {
			ObjectMapper mapper = ClientUtilities.getMapper();
			jsonConcepts = mapper.readTree(res);	
		} else {
			jsonConcepts = null;
		}
		
		System.out.println(jsonConcepts);
		return jsonConcepts;
		
	}
	
	private String getSearch(WebResource searchCall){
		String res = null;
		try{
			res = searchCall.accept(MediaType.APPLICATION_JSON_TYPE).cookie(ClientUtilities.cookie).get(String.class);
		}catch(Exception e){
			if (e instanceof UniformInterfaceException){
				res = getSearch(searchCall);
			}else{
				e.printStackTrace();
			}
		}
		
		return res;
		
	}
}
